﻿using OnlineOrder.Entity;
using OnlineOrder.UI;
using OnlineOrder.UI.管理员窗口;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace OnlineOrder
{
    public partial class LoginForm : Form
    {
        public static string LoginUserName = "";
        public LoginForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LoginUserName= textBox1.Text;
            //登录按钮
            string username = textBox1.Text;
            string password = textBox2.Text;
           

            //判断是否非空
            if (username == "")
            {
                MessageBox.Show("请输入用户名");
            } else if (password == "")
            {
                MessageBox.Show("请输入密码");
            }
            else 
            {
                try
                {
                    //sql语句
                    string sql = string.Format($"select Count(*) from [User] where username='{username}' and password='{password}'");

                    string sql2 = string.Format($"select Count(*) from Manager where username='{username}' and password='{password}'");
                    //查询用户名是否存在
                    int count = Convert.ToInt32(Utils.SqlHelper.ExecuteScalar(sql).ToString());

                    int count2 = Convert.ToInt32(Utils.SqlHelper.ExecuteScalar(sql2).ToString());

                    //判断如果count大于0则存在用户
                    if (count > 0)
                    { 

                            //显示XX用户登录成功
                            MessageBox.Show($"{username}用户登录成功！");
                        //从登录界面跳转到主界面
                        Middle.a = this.textBox1.Text;
                        MainForm mian = new MainForm();
                            this.Hide();
                            mian.ShowDialog();
                            this.Close();
       
                    }
                    if(count2 > 0)
                    {
                        //显示XX用户登录成功
                        MessageBox.Show($"{username}商家登录成功！");
                        //从登录界面跳转到主界面
                        Middle.a =this.textBox1.Text;
                        ManagerForm main =new ManagerForm();
                        this.Hide();
                        main.ShowDialog();
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("用户名或密码错误！");
                    }
                }
                catch (Exception)
                {

                    throw;
                }
            }
            
            

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //窗体加载
            
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //注册账号
            RegisterForm registerForm = new RegisterForm();
            this.Hide();
            registerForm.ShowDialog();
            this.Close();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //找回密码
            ChangePwd changePwd = new ChangePwd();
            this.Hide();
            changePwd.ShowDialog();
            this.Close();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
